﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 用于toggle切换能够响应事件的对象的交互功能
    /// </summary>
    [AddComponentMenu("Nirvana/UI/Control/Toggle Interactable")]
    [RequireComponent(typeof(Toggle))]
    public sealed class ToggleInteractable : MonoBehaviour
    {
        [SerializeField]
        private Selectable[] actives;
        [SerializeField]
        private Selectable[] deactives;
        private Toggle toggle;

        private void Awake()
        {
            this.toggle = base.GetComponent<Toggle>();
            Assert.IsNotNull<Toggle>(this.toggle);
            this.OnValueChanged(this.toggle.isOn);
            this.toggle.onValueChanged.AddListener(OnValueChanged);
        }

        private void OnEnable()
        {
            this.OnValueChanged(this.toggle.isOn);
        }

        private void OnValueChanged(bool isOn)
        {
            foreach (var p in this.actives)
            {
                p.interactable = isOn;
            }
            foreach (var p in this.deactives)
            {
                p.interactable = !isOn;
            }
        }

    }
}

